<?php
/**
 *用户统计逻辑层
 * 7.25
 * wsl
 */

namespace App\Http\Service\api;

use App\Http\Serve\Es;
use App\Http\Service\Redisd;
use App\Models\StoreOrder;
use App\Repository\Repositories\UserStatisticisRepositories;
use Illuminate\Validation\ValidationException;

class UserStatisticsService
{
    /**
     * @return array|string
     * 新增用户统计
     */
    public static function UserAddStatistics()
    {
        try {
            $user_data = [];
            for ($i=0;$i<15;$i++){
                $begin = strtotime(date("Y-m-d",strtotime("-$i day")));
                $end =  $begin+60*60*24-1;
                array_push($user_data,count((new UserStatisticisRepositories())->UserAddStatistics($begin,$end)));
            }
            return Array_reverse($user_data);
        } catch (ValidationException $validationException) {
            return $validationException->getFile();
        }
    }

    /**
     * @param $user_recharge
     * @return int|mixed
     * 用户累计充值
     */
    public static function userRecharge($user_recharge)
    {
        try {
        $num = 0;
        foreach ($user_recharge as $value){
            $num = $num+$value->price;
        }
        return $num;
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @param $user_recharge
     * @return array
     * 用户充值记录
     */
    public static function userRechargeRecording($user_recharge)
    {
        try {
        $array = [];
        foreach ($user_recharge as $key => $value){
            $array[$key] = $value->price;
        }
        return $array;
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @param $user_id
     * @return array|string
     * 用户支付统计
     */
    public static function paymentStatistics($user_id)
    {
        try {
            $user_data = [];
            for ($i=0;$i<=14;$i++){
                $begin = strtotime(date("Y-m-d",strtotime("-$i day")));
                $end =  $begin+60*60*24-1;
                $pay_total = (new UserStatisticisRepositories())->paymentStatistics($user_id,$begin,$end);
                $num = 0;
                foreach ($pay_total as $value){
                    $num = $num+$value->total_price;
                }
                array_push($user_data,$num);
            }
            $arr = Array_reverse($user_data);
             array_pop($arr);
            return $arr;
        } catch (ValidationException $validationException) {
            return $validationException->getFile();
        }
    }

    /**
     * @param $user_id
     * @return int|mixed|string
     * 用户累计支付
     */
    public static function paymentStatisticsTotal($user_id)
    {
        try {
        $num = 0;
        $pay_arr = self::paymentStatistics($user_id);
        foreach ($pay_arr as $value){
            $num = $num+$value;
        }
        return $num;
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @param $user_id
     * @return int|mixed|string
     * 用户支付环比计算
     */
    public static function paymentStatisticsChain($user_id)
    {
        try {
            $last_issue = 0;//上期
            $pay_arr = self::paymentStatistics($user_id);
            for ($i=0 ; $i<7 ; $i++){
                $last_issue = $last_issue+$pay_arr[$i];
            }
            $this_issue = 0;//本期
            for ($j=7 ; $j<14 ; $j++){
                $this_issue = $this_issue+$pay_arr[$j];
            }
            //计算环比增长率
            $chain = (($this_issue-$last_issue)/$last_issue*100);
            return number_format($chain,2).'%';
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @param $user_message
     * @return array
     * 用户充值排行榜
     */
    public static function userRechargeList($user_message)
    {
        try {
        $user_arr = [];
        $user_recharge_arr = [];
        //获取所有用户id
        foreach ($user_message as $key => $value){
            $user_arr[$key] = $value->uid;
        }
        //获取每个用户充值过多少钱
        foreach ($user_arr as $key => $value){
            $user_price_total = 0;
           $recharge_arr = (new UserStatisticisRepositories())->userRecharge($value);
           foreach ($recharge_arr as $k => $v){
                $user_price_total = $user_price_total+$v->price;
           }
            $user_recharge_arr[$value] = $user_price_total;
        }
        //把充值金额不为0的用户放到redis有序集合
        foreach (Array_diff($user_recharge_arr,[0]) as $key => $value){
            Redisd::getInstance()->zAdd('user_payment',[$value.'.'.time() => $key]);
        }
        //把所有用户充值数据从有序集合中取出
        $user_recharge_arr_last = [];
        foreach (Array_reverse(Redisd::getInstance()->zRange('user_payment',0,-1)) as $key => $value){
            //添加用户信息重新赋值
            $user_recharge_arr_last[$key][$value] = (new UserStatisticisRepositories())->searchUserName($value)->real_name;
        }
        return $user_recharge_arr_last;
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }

    /**
     * @param $status
     * @return int
     * 更改秒杀商品状态
     */
    public static function spikeStatus($status){
        try {
            if ($status == 1){
                return 0;
            }else{
                return 1;
            }
        }catch (ValidationException $validationException){
            return $validationException->getFile();
        }
    }
}
